package com.cjj.thread;

/**
 * @ClassName ThreadStudy_api
 * @Date 2021/06/28 22:11:56
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
public class ThreadStudy_api_2 {
    public static void main(String[] args) {
        Thread thread_1 = new Thread(new ThreadDemo2());
        thread_1.setName("线程-1");
        thread_1.start();
        Thread thread_2 = new Thread(new ThreadDemo2());
        thread_2.setName("线程-2");
        thread_2.start();
    }
}

class ThreadDemo2 implements Runnable{
    @Override
    /**
     * 在run方法中不能抛出异常，只能try catch
     */
    public void run() {
        for(int i = 0; i < 10; i++){
            try {
                // sleep：让当前线程从运行状态变为休眠状态，如果时间到期，继续进入运行状态
                // sleep不能释放锁
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                //e.printStackTrace();
            }
            // 获取到线程ID，getId，ID是多线程随机分配不重复主键
            System.out.println("获取当前线程ID:" + Thread.currentThread().getId() + "\t 获取当前线程名称:" + Thread.currentThread().getName());
        }
    }
}
